import 'reflect-metadata';
export let autowired = function (target, propertyKey: string) {
    const container = this;

    console.log('autowired start')

    let t = Reflect.getMetadata('design:type', target, propertyKey);
    if (!t) {
        // Needed to support react native inheritance
        t = Reflect.getMetadata('design:type', target.constructor, propertyKey);
    }

    const propKey = `__${propertyKey}`;
    Object.defineProperty(target.constructor.prototype, propertyKey, {
        enumerable: true,
        get: function () {
            return container.resolve(t.name);
        },
        set: function (newValue) {
            this[propKey] = newValue;
        }
    });

}



// export let autowired = function (target, propertyKey: string) {
//     const container = this;

//     console.log('autowired start')

//     let t = Reflect.getMetadata('design:type', target, propertyKey);
//     if (!t) {
//         // Needed to support react native inheritance
//         t = Reflect.getMetadata('design:type', target.constructor, propertyKey);
//     }
//     const constructor = target.constructor;
//     target.constructor = function(){
//         constructor();
//         target[propertyKey] = container.resolve(t.name);
//     }

    // const propKey = `__${propertyKey}`;
    // Object.defineProperty(target.constructor.prototype, propertyKey, {
    //     enumerable: true,
    //     get: function () {
    //         return container.resolve(t.name);
    //     },
    //     set: function (newValue) {
    //         this[propKey] = newValue;
    //     }
    // });

//}

export default function (container) {
    autowired = autowired.bind(container);
}